#include"Lightbar.hpp"


Lightbar::Lightbar(const RotatedRect &rectangle){// 这里的image仅是测试用的
        this->corners.resize(4); // 初始化容器
        rectangle.points(&corners[0]); // 提取角点
        sort(corners.begin(),corners.end(),[](const Point2d &a,const Point2d &b){
                return a.y < b.y;
        });

        // 画出框选
        // tools::draw_contours(image,corners);
        
        //--------------------标顶点和底点--------------
        this->topPoint = (corners[0]+corners[1])/2;
        this->bottomPoint = (corners[3]+corners[2])/2;
        this->midPoint = (topPoint + bottomPoint)/2;

        // this->t2bPoint.push_back(topPoint);
        // this->t2bPoint.push_back(bottomPoint);
        // circle(image,topPoint,4,Scalar(0,255,0),-1);
        // circle(image,bottomPoint,4,Scalar(0,255,0),-1);
        // putText(image, to_string(topPoint.x)+" , "+to_string(topPoint.y), topPoint, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,255,0), 1);
        // putText(image, to_string(bottomPoint.x)+" , "+to_string(bottomPoint.y), bottomPoint, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,255,0), 1);
        //--------------------------------------------

        this->top2bottom = bottomPoint - topPoint; // 向量 top -> bottom
        this->height = norm(top2bottom); // 这里求灯条长度
        this->width = max(norm(corners[0] - corners[1]),norm(corners[2] - corners[3]) ); // 宽度
        // 比例
        this->ratio = height/width; 
        // 计算倾斜角
        this->angle = atan2(top2bottom.y ,top2bottom.x); 
        this->error_angle = abs(angle - CV_PI / 2); 
        

        // putText(image,"ratio: "+to_string(this->ratio),topPoint+Point2f(0,-30),FONT_HERSHEY_SIMPLEX,1,Scalar(250,100,55),1);
        // putText(image,to_string(this->error_angle),bottomPoint+Point2f(30,15),FONT_HERSHEY_SIMPLEX,1,Scalar(200,100,55),1);
        // putText(image,"height: "+to_string(this->height),midPoint+Point2f(-30,50),FONT_HERSHEY_SIMPLEX,1,Scalar(0,255,0),2);
        // putText(image,"width: "+to_string(this->width),midPoint+Point2f(-30,-50),FONT_HERSHEY_SIMPLEX,1,Scalar(255,0,0),2);
        // cout<<"角度："<<error_angle*57.3<<"\n"
        //         <<"长度1: "<<height<<"\n"
        //         <<"宽度1: "<<width<<"\n"
        //         <<"比例: "<<ratio<<endl;
        //         cout<<endl;
    
    }


//判断灯条 
bool Lightbar::lightbar_decide(){
        auto angle_ok = (this->error_angle * 57.3) < 45;  
        auto ratio_ok = this->ratio > 3.0 && this->ratio < 35;
        // auto length_ok = this->height > 8; // 太小的不要
        return angle_ok & ratio_ok ;
}

